home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / rpg / crossfir.92 / crossfir / crossfire-0.92.5 / server / c_party.c < prev    next >
C/C++ Source or Header  |  1996-07-24  |  12KB  |  403 lines

  1. /*
  2.  * static char *rcsid_input_c =
  3.  *   "$Id: c_party.c,v 1.7 1996/01/02 11:34:39 master Exp $";
  4.  */
  5. /*
  6.     CrossFire, A Multiplayer game for X-windows
  7.  
  8.     Copryight (C) 1994 Mark Wedel
  9.     Copyright (C) 1992 Frank Tore Johansen
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25.     The author can be reached via e-mail to master@rahul.net
  26. */
  27.  
  28. #include <global.h>
  29. #ifndef __CEXTRACT__
  30. #include <sproto.h>
  31. #endif
  32. #include <version.h>
  33. #include <main.h>
  34. #include <spells.h>
  35.  
  36. #ifdef SIMPLE_PARTY_SYSTEM
  37. static partylist * firstparty=NULL; /* Keeps track of first party in list */
  38. static partylist * lastparty=NULL; /*Keeps track of last party in list */
  39.  
  40. partylist * form_party(object *op, char *params, partylist * firstparty, partylist * lastparty) {
  41.  
  42.     partylist * newparty;
  43.     int nextpartyid;
  44.  
  45.     if(firstparty == NULL) {
  46.         nextpartyid = 1;
  47.     } else {
  48.         nextpartyid = lastparty->partyid;
  49.         nextpartyid++;
  50.     }
  51.  
  52.     newparty = (partylist *)malloc(sizeof(partylist));
  53.     newparty->partyid = nextpartyid;
  54.     newparty->partyname = strdup_local(params);
  55.  
  56.     newparty->passwd[0] = '\0';
  57.     newparty->next = NULL;
  58.     newparty->partyleader = (char *)malloc(strlen(op->name));
  59.     newparty->partyleader = strcpy(newparty->partyleader,op->name);
  60.     if(firstparty != NULL)
  61.         lastparty->next = newparty;
  62.     new_draw_info_format(NDI_UNIQUE, 0, op,
  63.     "You have formed party: %s",newparty->partyname);
  64.     return newparty;
  65. }
  66.  
  67. char * find_party(int partynumber, partylist * party) {
  68.  
  69.     while(party != NULL) {
  70.         if(party->partyid == partynumber)
  71.             return party->partyname;
  72.         else
  73.             party = party->next;
  74.     }
  75.     return NULL;
  76. }
  77. #if 0
  78. void get_party_passwd(object *op) {
  79.     op->contr->write_buf[0]='\0';
  80.     op->contr->writing=0;
  81.     op->contr->state=ST_GET_PASSWORD;
  82.     draw_info(op,"What is your password?");
  83.     write_ch(op,':');
  84.     op->contr->no_echo=1;
  85. }
  86. #endif
  87.  
  88. int confirm_party_password(object *op) {
  89.   partylist * tmppartylist;
  90.  
  91.   tmppartylist = firstparty;
  92.   while(tmppartylist != NULL) {
  93.     if(op->contr->party_number_to_join == tmppartylist->partyid) {
  94.         if(strcmp(op->contr->write_buf+1,tmppartylist->passwd) == 0)
  95.           return 0;
  96.         else
  97.           return 1;
  98.     }
  99.     tmppartylist = tmppartylist->next;
  100.   }
  101.   return 1;
  102. }
  103.  
  104. void receive_party_password(object *op, char k) {
  105.  
  106.   if(k!=13) {
  107.     if((k==8 || k==127)&&(op->contr->writing==1))
  108.       return;
  109.     if(k!=8 && k !=127 && (!((k>='a' && k<='z') || (k>='A' && k<='Z')) ||
  110.                            op->contr->writing > 8))
  111.       return;
  112.     write_ch(op,k);
  113.     return;
  114.   }
  115.   op->contr->writing = 0;
  116.   if(confirm_party_password(op) == 0) {
  117.     op->contr->party_number = op->contr->party_number_to_join;
  118.     op->contr->party_number_to_join = (-1);
  119.     new_draw_info_format(NDI_UNIQUE, 0,op,
  120.     "You have joined party: %s\n",find_party(op->contr->party_number
  121.     ,firstparty));
  122.     op->contr->state = ST_PLAYING;
  123.     return;
  124.   }
  125.   else {
  126.     new_draw_info(NDI_UNIQUE, 0,op,"You entered the wrong password");
  127.     op->contr->party_number_to_join = (-1);
  128.     op->contr->state = ST_PLAYING;
  129.     return;
  130.   }
  131. }
  132.  
  133. void send_party_message(object *op,char *msg)
  134. {
  135.   player *pl;
  136.   int no=op->contr->party_number;
  137.   for(pl=first_player;pl!=NULL;pl=pl->next)
  138.     if(pl->ob->contr->party_number==no && pl->ob!=op)
  139.     new_draw_info(NDI_UNIQUE, NDI_WHITE, pl->ob, msg);
  140. }
  141.  
  142. int command_gsay(object *op, char *params)
  143. {
  144.   char party_params[MAX_BUF];
  145.  
  146.   if (!params) return 0;
  147.   strcpy(party_params, "say "); 
  148.   strcat(party_params,params);
  149.   command_party(op,party_params);
  150.   return 0;
  151. }
  152.  
  153.  
  154. int command_party (object *op, char *params)
  155. {
  156.   char buf[MAX_BUF];
  157.   partylist * tmpparty; /* For iterating over linked list */
  158.   char * currentparty; /* For iterating over linked list */
  159.  
  160.   if(params == NULL) {
  161.         if(op->contr->party_number<=0) {
  162.           new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of any party.");
  163.           new_draw_info(NDI_UNIQUE, 0,op,"For help try: party help");
  164.         }
  165.         else {
  166.           currentparty = find_party(op->contr->party_number,firstparty);
  167.       new_draw_info_format(NDI_UNIQUE, 0, op,
  168.         "You are a member of party %s.", currentparty);
  169.         }
  170.         return 1;
  171.       }
  172.   if(strcmp(params, "help")==0) {
  173.     new_draw_info(NDI_UNIQUE, 0,op,"To form a party type: party form <partyname>");
  174.     new_draw_info(NDI_UNIQUE, 0,op,"To join a party type: party join <partyname>");
  175.     new_draw_info(NDI_UNIQUE, 0,op,"If the party has a passwd, it will you prompt you for it.");
  176.     new_draw_info(NDI_UNIQUE, 0,op,"For a list of current parties type: party list");
  177.     new_draw_info(NDI_UNIQUE, 0,op,"To leave a party type: party leave");
  178.     new_draw_info(NDI_UNIQUE, 0,op,"To change a passwd for a party type: party passwd <password>"\
  179. );
  180.     new_draw_info(NDI_UNIQUE, 0,op,"There is an 8 character max");
  181.     new_draw_info(NDI_UNIQUE, 0,op,"To talk to party members type: party say <msg>");
  182.     new_draw_info(NDI_UNIQUE, 0,op,"To see who is in your party: party who");
  183.     return 1;
  184.   }
  185.   if(strncmp(params, "say ", 4)==0)
  186.     {
  187.           if(op->contr->party_number<=0)
  188.             {
  189.               new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of any party.");
  190.               return 1;
  191.             }
  192.       params += 4;
  193.       sprintf(buf, "%s says: %s", op->name, params);
  194.           send_party_message(op,buf);
  195.           new_draw_info(NDI_UNIQUE, 0,op,"Ok.");
  196.           return 1;
  197.         }
  198.  
  199.   if(strncmp(params, "form ",5) == 0) {
  200.  
  201.     params += 5;
  202.  
  203.     if(firstparty == NULL) {
  204.       firstparty = form_party(op, params, firstparty, lastparty);
  205.       lastparty = firstparty;
  206.       return 0;
  207.     }
  208.     else
  209.       tmpparty = firstparty->next;
  210.  
  211.     if(tmpparty == NULL) {
  212.       if(strcmp(firstparty->partyname, params) != 0) {
  213.         lastparty = form_party(op, params, firstparty, lastparty);
  214.         return 0;
  215.       }
  216.       else {
  217.     new_draw_info_format(NDI_UNIQUE, 0,op,
  218.         "The party %s already exists, pick another name",params);
  219.         return 1;
  220.       }
  221.     }
  222.  
  223.     while(tmpparty != NULL) {
  224.       if(strcmp(tmpparty->partyname,params) == 0) {
  225.         sprintf("The party %s already exists, pick a new name",params);
  226.         return 1;
  227.       }
  228.       tmpparty = tmpparty->next;
  229.     }
  230.  
  231.     lastparty = form_party(op, params, firstparty, lastparty);
  232.     return 0;
  233.   } /* form */
  234.  
  235.   if(strcmp(params, "leave")==0) {
  236.     if(op->contr->party_number<=0)
  237.       {
  238.         new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of any party.");
  239.         return 1;
  240.       }
  241.     currentparty = find_party(op->contr->party_number,firstparty);
  242.     new_draw_info_format(NDI_UNIQUE, 0, op,
  243.     "You leave party %s.",currentparty);
  244.     sprintf(buf,"%s leaves party %s.",op->name,currentparty);
  245.     send_party_message(op,buf);
  246.     op->contr->party_number=-1;
  247.     return 1;
  248.   }
  249.   if(strcmp(params, "who")==0) {
  250.     player *pl;
  251.     int no=op->contr->party_number;
  252.     if(no<=0) {
  253.       new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of any party.");
  254.       return 1;
  255.     }
  256.     new_draw_info_format(NDI_UNIQUE, 0, op,
  257.     "Members of party: %s.",find_party(no,firstparty));
  258.     for(pl=first_player;pl!=NULL;pl=pl->next)
  259.       if(pl->ob->contr->party_number==no) {
  260. #ifdef SET_TITLE
  261.         if(pl->ob->contr->own_title[0]!='\0')
  262.           sprintf(buf,"%3d %s the %s",
  263.                   pl->ob->level,pl->ob->name,pl->ob->contr->own_title);
  264.         else
  265. #endif /* SET_TITLE */
  266.           sprintf(buf,"%3d %s the %s",
  267.                   pl->ob->level,pl->ob->name,pl->ob->contr->title);
  268.         new_draw_info(NDI_UNIQUE, 0,op,buf);
  269.       }
  270.     return 1;
  271.   } /* leave */
  272.  
  273.   if(strncmp(params, "passwd ", 7) == 0) {
  274.     partylist *tmplist;
  275.  
  276.     params += 7;
  277.  
  278.     if(op->contr->party_number <= 0) {
  279.       new_draw_info(NDI_UNIQUE, 0,op,"You are not a member of a party");
  280.       return 1;
  281.     }
  282.  
  283.     tmplist = firstparty;
  284.     while(tmplist != NULL) {
  285.       if(tmplist->partyid == op->contr->party_number) {
  286.         strncpy(tmplist->passwd,params,8);
  287.     new_draw_info_format(NDI_UNIQUE, 0, op,
  288.         "The password for party %s is %s"
  289.                 ,tmplist->partyname,tmplist->passwd);
  290.         send_party_message(op,buf);
  291.         return 0;
  292.       }
  293.       tmplist = tmplist->next;
  294.     }
  295.     return 0;
  296.   } /* passwd */
  297.  
  298.   if(strcmp(params, "list") == 0) {
  299.     partylist * tmplist;
  300.  
  301.     tmplist = firstparty;
  302.  
  303.     if(firstparty == NULL) {
  304.       new_draw_info(NDI_UNIQUE, 0,op,"There are no parties active right now");
  305.       return 1;
  306.     }
  307.  
  308.     new_draw_info(NDI_UNIQUE, 0,op,"Party name                       Leader");
  309.     new_draw_info(NDI_UNIQUE, 0,op,"----------                       ------");
  310.  
  311.     while(tmplist != NULL) {
  312.       new_draw_info_format(NDI_UNIQUE, 0,op,
  313.     "%-32s %s",tmplist->partyname
  314.               ,tmplist->partyleader);
  315.       tmplist = tmplist->next;
  316.     }
  317.     return 0;
  318.   } /* list */
  319.  
  320.   if(strncmp(params,"join ",5) == 0) {
  321.  
  322.     params += 5;
  323.  
  324.     /* Can't join a party cause non exist */
  325.     if(firstparty == NULL) {
  326.       new_draw_info_format(NDI_UNIQUE, 0, op,
  327.     "Party: %s does not exist.  You must form it first",params);
  328.       return 1;
  329.     }
  330.  
  331.     /* Special case if thier is only one party */
  332.     if(firstparty->next == NULL) {
  333.       if(strcmp(firstparty->partyname,params) != 0) {
  334.     new_draw_info_format(NDI_UNIQUE, 0,op,
  335.         "Party: %s does not exist. You must form it first",params);
  336.         return 1;
  337.       }
  338.       else {
  339.         if(op->contr->party_number == firstparty->partyid) {
  340.       new_draw_info_format(NDI_UNIQUE, 0, op,
  341.         "You are already in party: %s"
  342.                   ,firstparty->partyname);
  343.           return 1;
  344.         }
  345.         /* found party player wants to join */
  346.         if(firstparty->passwd[0] == '\0') {
  347.           op->contr->party_number = firstparty->partyid;
  348.       new_draw_info_format(NDI_UNIQUE, 0, op,
  349.         "You have joined party: %s",firstparty->partyname);
  350.           return 0;
  351.         }
  352.         else {
  353.           get_party_password(op,firstparty->partyid);
  354.           return 0;
  355.         }
  356.       }
  357.     }
  358.  
  359.     tmpparty = firstparty;
  360.     while(tmpparty != NULL) {
  361.       if(strcmp(tmpparty->partyname,params) == 0) {
  362.         if(op->contr->party_number == tmpparty->partyid) {
  363.       new_draw_info_format(NDI_UNIQUE, 0, op,
  364.         "You are already a member of party: %s"
  365.                   ,tmpparty->partyname);
  366.           return 1;
  367.         }
  368.         else {
  369.           if(tmpparty->passwd[0] == '\0') {
  370.         new_draw_info_format(NDI_UNIQUE, 0, op,
  371.         "You have joined party: %s",tmpparty->partyname);
  372.             op->contr->party_number = tmpparty->partyid;
  373.             return 0;
  374.           }
  375.           else {
  376.             get_party_password(op, tmpparty->partyid);
  377.             return 0;
  378.           }
  379.         }
  380.       }
  381.       else
  382.         tmpparty = tmpparty->next;
  383.     }
  384.  
  385.     new_draw_info_format(NDI_UNIQUE, 0,op,
  386.     "Party %s does not exist.  You must form it first.",params);
  387.     return 1;
  388.   } /* join */
  389.  
  390.   new_draw_info(NDI_UNIQUE, 0,op,"To form a party type: party form <partyname>");
  391.   new_draw_info(NDI_UNIQUE, 0,op,"If the party has a passwd, it will you prompt you for it.");
  392.   new_draw_info(NDI_UNIQUE, 0,op,"For a list of current parties type: party list");
  393.   new_draw_info(NDI_UNIQUE, 0,op,"To leave a party type: party leave");
  394.   new_draw_info(NDI_UNIQUE, 0,op,"To change a passwd for a party type: party passwd <password>");
  395.   new_draw_info(NDI_UNIQUE, 0,op,"There is an 8 character max");
  396.   new_draw_info(NDI_UNIQUE, 0,op,"To talk to party members type: party say <msg>");
  397.   new_draw_info(NDI_UNIQUE, 0,op,"To see who is in your party: party who");
  398.   return 1;
  399. }
  400.  
  401. #endif /* SIMPLE_PARTY_SYSTEM */
  402.  
  403.